package com.plateform.shardingsphere.config;




import cn.hutool.core.date.DateUtil;
import com.plateform.shardingsphere.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.sharding.api.sharding.standard.PreciseShardingValue;
import org.apache.shardingsphere.sharding.api.sharding.standard.RangeShardingValue;
import org.apache.shardingsphere.sharding.api.sharding.standard.StandardShardingAlgorithm;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.sql.Timestamp;
import java.util.*;

/**
 * @ClassName DateShardingAlgorithm
 * @Description: TODO
 * @Author liupantao
 * @Date 2022/10/20
 * @Version V1.0
 **/
//sharding分表规则
//按单月分表
@Component
@Slf4j
public class DateShardingAlgorithm implements StandardShardingAlgorithm<Timestamp> {
    // 查询使用
    @Override
    public Collection<String> doSharding(Collection<String> collection, RangeShardingValue<Timestamp> rangeShardingValue) {
        // 查询数据库中的表 hss_history
        List<String> tableNames = ShardingAlgorithmTool.getAllTableNameBySchema();
        HashSet<String> tableNameCache = ShardingAlgorithmTool.cacheTableNames();
        Timestamp startDate = rangeShardingValue.getValueRange().lowerEndpoint();
        Timestamp endDate = rangeShardingValue.getValueRange().upperEndpoint();
        String logicTableName = rangeShardingValue.getLogicTableName();
        String start = DateUtil.format(startDate, "yyyyMM");
        String end = DateUtil.format(endDate, "yyyyMM");
        List<String> trueTable = new ArrayList<>();
        List<String> noTable=new ArrayList<>();
        if(start.equals(end)&&tableNameCache.contains(logicTableName + start)){
            trueTable.add(logicTableName + start);
        }else{
            for (String table : tableNameCache) {
                if (CollectionUtils.isEmpty(trueTable)) {
                    if (trueTable.size() == 0) {
                        if(table.equals(logicTableName + start)){
                            trueTable.add(table);
                        }

                    } else {
                    }
                }else{
                    if(trueTable.size()==1){
                        if(table.equals(logicTableName + end)){
                            trueTable.add(table);
                            break;
                        }

                    }
                }
            }
        }
        // 返回数据库实时存在的表 如果返回collection会提示表不存在
        if(CollectionUtils.isEmpty(trueTable)){
            String s = ShardingAlgorithmTool.shardingTablesCheckAndCreatAndReturn(logicTableName, logicTableName + start);
            trueTable.add(s);
            return  trueTable;
        }
        return trueTable;
    }

    // 添加使用
    @Override
    public String doSharding(Collection<String> collection, PreciseShardingValue<Timestamp> preciseShardingValue) {
        StringBuilder resultTableName = new StringBuilder();
        String logicTableName = preciseShardingValue.getLogicTableName().toLowerCase();
        //表名精确匹配，表名加上截取的时间
        resultTableName.append(logicTableName)
                //时间戳秒级转毫秒级转成date类型
                .append("").append(DateUtils.format(new Date(preciseShardingValue.getValue().getTime()), DateUtils.YEAR_MONTH_NUMBER));
        //System.out.println("插入表名为:" + resultTableName);
        return ShardingAlgorithmTool.shardingTablesCheckAndCreatAndReturn(logicTableName, resultTableName.toString());
    }

    @Override
    public void init() {

    }

    @Override
    public String getType() {
        //　自定义 这里需要spi支持
        return null;
    }
}


